package com.example.moonshine.baidumap.Util;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class HttpUtil {
    OkHttpClient client = new OkHttpClient();
    String result = null;
    //登陆请求
    public String GetPostLogin(String uname, String upsd, String path) {
        //okhttp Post请求传输Json数据

//        MediaType JSON = MediaType.parse("application/json; charset = utf-8");
        FormBody body = new FormBody.Builder()
                .add("username", uname)
                .add("password", upsd)
                .add("mode", "app")
                .build();
        Request request = new Request.Builder()
                .url(path)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    //上传gps
    public String getPostGps(String name,String gps,String path,String token){

//        MediaType JSON = MediaType.parse("application/json; charset = utf-8");
        FormBody body = new FormBody.Builder()
                .add("gpsPoint", gps)
                .add("userId", name)
                .build();
        Request request = new Request.Builder()
                .addHeader("accessToken",token)
                .url(path)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    //查看任务
    public String getPostTask(String user,String token,String path){
        FormBody body = new FormBody.Builder()
                .add("userId", user)
                .build();
        Request request = new Request.Builder()
                .addHeader("accessToken",token)
                .url(path)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    //接受任务
    public String getPostAcceptTask(String user,String enentId,String type,String token,String path){
        FormBody body = new FormBody.Builder()
                .add("userId", user)
                .add("eventId",enentId)
                .add("type",type)
                .build();
        Request request = new Request.Builder()
                .addHeader("accessToken",token)
                .url(path)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    // 完成任务
    public String getPostFinishTask(String user,String token,String path){
        FormBody body = new FormBody.Builder()
                .add("userId", user)
                .build();
        Request request = new Request.Builder()
                .addHeader("accessToken",token)
                .url(path)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
